package com.hz.house.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hz.house.dao.CallPhoneMapper;
import com.hz.house.utils.ex.FlowException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import sun.security.provider.MD5;

import java.security.MessageDigest;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

@Service
@Slf4j
public class PhoneCodeService {
    @Autowired
    private RestTemplate restTemplate;

    public String encodePhoneNumber(String localPhone, String remotePhone, String callUrl, String param){
        String url = "http://120.78.14.88/webapi.php?act=call";

        Map<String, Object> map = sign(localPhone, remotePhone, null, callUrl, null, param, null);
        StringBuilder builder = new StringBuilder(url);
        for (Map.Entry<String, Object> entry:map.entrySet()){
            builder.append("&").append(entry.getKey()).append("=").append(Objects.isNull(entry.getValue()) ? "": entry.getValue());
        }
        System.out.println(builder.toString());
        String res = new RestTemplate().getForObject(builder.toString(), String.class);
        JSONObject json = JSON.parseObject(res);
        if (json.getIntValue("code") == 1){
            JSONObject data =  json.getJSONObject("data");
            if (Objects.nonNull(data)){
                return data.getString("showno");
            }
        }
        throw new FlowException(json.getString("msg"));
    }

    private Map<String, Object> sign(String mynumber, String callnumber, Integer fee_type, String return_url, Integer max, String params, String prefix){
        String appId = "21207";
        String appKey = "98956245";

        TreeMap<String, Object> map = new TreeMap<>();
        map.put("appid", appId);
        map.put("mynumber", mynumber);
        map.put("return_url", return_url);
        map.put("max", max);
        map.put("params", params);
        map.put("callnumber", callnumber);
        map.put("fee_type", fee_type);
        map.put("prefix", prefix);

        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, Object> entry:map.entrySet()){
            builder.append("&").append(entry.getKey()).append("=").append(Objects.isNull(entry.getValue()) ? "": entry.getValue());
        }
        builder.append(appKey);
        String sign = MD532(builder.toString());
        map.put("sign", sign);
        return map;
    }


    private String MD532(String source){
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md5.digest(source.getBytes());
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16)
                    hexValue.append("0");
                hexValue.append(Integer.toHexString(val));
            }
            source = hexValue.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return source;
    }

    public static void main(String[] args) {
//        new PhoneCodeService().sign("", "", null, "", null, "", "");
        JSONObject json = new JSONObject();
        json.put("id", 100);
        //http://118.24.85.122:8088/callBack/encodePhoneCallBack
        String number = new PhoneCodeService().encodePhoneNumber("15828676169", "19141906769", "http://118.24.85.122:8088/phone/callBack", "999");
        System.out.println(number);
    }
}
